﻿import re

unit_names = [u"个",u"包",u"台",u"瓶",u"篮",u"杯",u"本",u"根",u"条",u"屉",u"笼",u"只",u"把",u"张",u"盘",u"袋",u"听",u"枚"]

weight_names = [u"(公斤)",u"斤半",u"斤",u"两半",u"两",u"(千克)",u"克",u"kg",u"g",u"(立方米)",u"(公升)",u"升"]

number_names = [u"零",u"一",u"二",u"三",u"四",u"五",u"六",u"七",u"八",u"九",u"壹",u"贰",u"叁",u"肆",u"伍",u"陆",u"柒",u"捌",u"玖",u"半"]

quantityunit_names = [u"亿",u"(千万)",u"(百万)",u"(十万)",u"万",u"千",u"仟",u"百",u"佰",u"十",u"拾"]

money_names = [u"(美元)",u"(美刀)",u"刀",u"$",u"(美分)",u"元",u"块",u"角",u"毛",u"分",u"￥"]

time_names = [u"点",u"时",u"刻",u"分",u"秒",u"(毫秒)",u"(微秒)"]



def getAnnotationList(message):
    markList = []
    
    digit_re = u"((\d*\.?\d+)"+ u"(" + u"|".join(quantityunit_names) + u")?)+"
    quantity_re = u"(" + u"|".join(number_names) + u"|" + u"|".join(quantityunit_names) + u")+"
    weight_re = u"(" + u"|".join(weight_names) + u")+"
    unit_re = u"(" + u"|".join(unit_names) + u")+"
    money_re = u"(" + u"|".join(money_names) + u")+"
    
    qutity_weight_re = u"((" + digit_re + u"|" + quantity_re + u")" + weight_re + u")+"
    #print qutity_weight_re
    matched_objs = re.finditer(qutity_weight_re, message, re.UNICODE|re.MULTILINE)
    for matched_obj in matched_objs:
        #print matched_obj.span(), matched_obj.group()
        markList.append((matched_obj.span(), u"qutity_weight", matched_obj.group()))
        
    qutity_money_re = u"((" + digit_re + u"|" + quantity_re + u")" + money_re + u")+"
    #print qutity_money_re
    matched_objs = re.finditer(qutity_money_re, message, re.UNICODE|re.MULTILINE)
    for matched_obj in matched_objs:
        #print matched_obj.span(), matched_obj.group()
        markList.append((matched_obj.span(), u"qutity_money", matched_obj.group()))
    
    qutity_unit_re = u"((" + digit_re + u"|" + quantity_re + u")" + unit_re + u")+"
    #print qutity_unit_re
    matched_objs = re.finditer(qutity_unit_re, message, re.UNICODE|re.MULTILINE)
    for matched_obj in matched_objs:
        #print matched_obj.span(), matched_obj.group()
        markList.append((matched_obj.span(), u"qutity_unit", matched_obj.group()))
        
    #print markList
    return sorted(markList)
    
def getMarkName(char):
    unit_names_set = set(unit_names)
    if char in unit_names_set:
        return "unit"

    weight_names_set = set(weight_names)
    if char in weight_names_set:
        return "weight"

    number_names_set = set(number_names)
    if char in number_names_set:
        return "number"

    quantityunit_names_set = set(quantityunit_names)
    if char in quantityunit_names_set:
        return "quantityunit"

    money_names_set = set(money_names)
    if char in money_names_set:
        return "money"
    
    try:
        if char and (int(char) or char == u"."):
            return "digit"
    except:
        pass
    
    return "None"